<?php
	/**
	 * General purpose database functions
	 * 
	 * @author Austin Laugesen <austin-laugesen@uiowa.edu>
	 * @version 1.0
	 * @package unknown
	 */
	require_once('config.php');
	
	/**
	 * Create a connection to the default database 
	 * @return database connection
	 */
	function getDBconnection(){
		global $DB_HOST, $DB_USERNAME, $DB_PASS, $DB_NAME;
		
		// connect to a MySQL server and select a database:
		$connection = mysql_connect($DB_HOST,$DB_USERNAME, $DB_PASS);
		
		if(!isset($connection)){
			die('Unable to connect to the server');
		}
		if(!mysql_select_db($DB_NAME)){
			die('Unable to select the database');
		}
		
		return $connection;
	}
	
	/**
	 * Build a SQL query statement
	 * @param string $tableName
	 * @param array $arr
	 * @return string
	 */
	function buildSelectStatement($tableName, $arr){
		
		$sql = 'SELECT * FROM ' . $tableName;
		
		if(count($arr) > 0){
			$sql .= ' WHERE';
			foreach ($arr as $column => $value){
				$sql .= ' ' . $column . ' = \'' . $value . '\', ';
			}
			
			// delete the last comma and concatenate a semicolon!
			$sql = substr($sql, 0, -2);
		}
		$sql .= ';';
		
		return $sql;
	}
	
	function getMaxID($table){
		$query = 'SELECT MAX(id) FROM ' . $table . ';';
		
		$maxID = executeSQL($query, 'Failed to retrieve the new user\'s ID.');
		
		$maxID = mysql_fetch_array($maxID);
		return $maxID[0];
	}
	
	/**
	 * Build a SQL query statement
	 * @param string $tableName
	 * @param array $arr
	 * @return string
	 */
	function buildInsertStatement($tableName, $arr){
		
		$sql = 'INSERT INTO ' . $tableName . ' ( ';
		foreach ($arr as $column => $value){
			$sql .= ' ' . $column . ', ';
		}		
		// delete the last comma and concatenate a semicolon!
		$sql = substr($sql, 0, -2) . ') VALUES (';
		
		foreach ($arr as $column => $value){
			$sql .= ' \'' . $value . '\', ';
		}		
		// delete the last comma and concatenate a semicolon!
		$sql = substr($sql, 0, -2) . ');';
		
		return $sql;
	}
	
	function buildDeleteStatement($tableName, $id){
		$sql = 'DELETE FROM ' . $tableName . ' WHERE id=' . $id;
		return $sql;
	}
	
	/**
	 * Build a SQL query statement
	 * @param string $tableName
	 * @param array $arr
	 * @param string $id
	 * @return string
	 */
	function buildUpdateStatement($tableName, $arr, $id){
		
		$sql = 'UPDATE ' . $tableName . ' SET ';
		foreach ($arr as $column => $value){
			$sql .= ' ' . $column . '=\'' . $value . '\', ';
		}		
			
		// delete the last comma and concatenate a semicolon!
		$sql = substr($sql, 0, -2) . ' WHERE id= '. $id . ';';

		return $sql;
	}
	
	/**
	 * Build a SQL query statement
	 * @param string $query
	 * @param dbConnection $conn
	 * @return mySQLResult
	 */
	function executeSQL($query, $failMessage = ''){
		
		// Connect from the database
		$conn = getDBconnection();
		//echo $query;
		$result = mysql_query($query, $conn);
		
		// verify that the database returned a value from query!
		if(false == $result){
			die($failMessage);
		}

		// Disconnect from the database
		if(true == isset($conn)){
			mysql_close($conn);
		}

		return $result;
	}
		
	/**
	 * This function retrieves an associative array that contains info about the specified user.
	 * @return array
	 */
	function getUserInfo($id){
		// build a SQL query
		$query = buildSelectStatement('user', array('id' => $id));
		
		$result = executeSQL($query, 'Sorry, we\'re unable to determine who is logged in.');
		
		// document why mysql_fetch_assoc is used!
		return mysql_fetch_assoc($result);
	}
	
	/**
	 * This function retrieves an associative array that contains info about the currently logged in user.
	 * @return array
	 */
	function getLoggedInUser(){
		return getUserInfo($_SESSION['id']);
	}
	
	/**
	 *  
	 * @return I have to check...
	 */
	function getAccounts($userid){		
		$query = buildSelectStatement('account', array('userid'=> $userid)); 
		//echo $queryStr;
				
		$result = executeSQL($query, 'Sorry, we\'re unable to retreive the list of accounts you requested.');

		return $result;
	}
	
?>